package com.example.redisdemo.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.redisdemo.aop.annotation.AutoDoubleDelRedisData;
import com.example.redisdemo.entity.Record;
import com.example.redisdemo.mapper.RecordMapper;
import com.example.redisdemo.service.RecordService;
import com.example.redisdemo.utils.RedisUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * @ProjectName: redisdemo
 * @Package: com.example.redisdemo.service.impl
 * @ClassName: RecordServiceImpl
 * @Author: zlx
 * @Description:
 * @Date: 2022/6/18 0:42
 * @Version: 1.0
 */
@Service
public class RecordServiceImpl extends ServiceImpl<RecordMapper, Record> implements RecordService {

    /**
     * 新增记录
     * zlx
     * 10:04 2022/6/18
     * @param record 记录内容
     * @return com.example.redisdemo.entity.Record
     **/
    @Override
    public Record addRecord(Record record) {
        record.setRecordId(IdUtil.fastSimpleUUID());
        record.setRecordCode(RedisUtils.increment(LocalDate.now() + "RECORDCODE",1,0));
        record.setCreateTime(LocalDateTime.now());
        if (this.save(record)) {
            return record;
        }
        return null;
    }

    /**
     * 根据记录id删除记录
     * zlx
     * 10:09 2022/6/18
     * @param recordId 记录id
     * @return boolean
     **/
    @Override
    @AutoDoubleDelRedisData(prefixs = "RECORD:",keyFields = "self")
    public boolean delRecord(String recordId) {
        return this.removeById(recordId);
    }

    /**
     * 修改记录内容
     * zlx
     * 10:06 2022/6/18
     * @param record    修改记录内容
     * @return com.example.redisdemo.entity.Record
     **/
    @Override
    @AutoDoubleDelRedisData(prefixs = "RECORD:",keyFields = "recordId")
    public Record putRecord(Record record) {
        record.setUpdateTime(LocalDateTime.now());
        if (this.updateById(record)) {
            return record;
        }
        return null;
    }

    /**
     * 分页获取记录列表
     * zlx
     * 10:11 2022/6/18
     * @param page      分页信息
     * @param record    记录查询信息
     * @return com.baomidou.mybatisplus.core.metadata.IPage<com.example.redisdemo.entity.Record>
     **/
    @Override
    public IPage<Record> getRecordPage(Page page, Record record) {
        QueryWrapper<Record> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(record.getRecordId())) {
            queryWrapper.lambda().eq(Record::getRecordId,record.getRecordId());
        }
        if (StrUtil.isNotBlank(record.getRecordCode())) {
            queryWrapper.lambda().eq(Record::getRecordCode,record.getRecordCode());
        }
        if (StrUtil.isNotBlank(record.getRecordContent())) {
            queryWrapper.lambda().like(Record::getRecordContent,record.getRecordContent());
        }
        return this.baseMapper.selectPage(page,queryWrapper);
    }

    /**
     * 根据记录id获取记录详情
     * zlx
     * 10:12 2022/6/18
     * @param recordId  记录id
     * @return com.example.redisdemo.entity.Record
     **/
    @Override
    public Record getRecordById(String recordId) {
        String redisKey = "RECORD:" + recordId;
        Record record = RedisUtils.hashGetAll(redisKey,Record.class);
        if (record == null) {
            QueryWrapper<Record> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(Record::getRecordId, recordId);
            record = this.getOne(queryWrapper);
            if (record != null) {
                RedisUtils.hashPutAll(redisKey, record, 300);
            }
        }
        return record;
    }
}
